package com.zl.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zl.payment.entity.HistoryBill;
import com.zl.payment.mapper.HistoryBillMapper;
import com.zl.payment.service.IHistoryBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zl.payment.service.IRepaymentRecordService;
import com.zl.payment.util.DateUtils;
import com.zl.payment.util.PaymentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;


/**历史账单为用户在还款后对未完成的账单生成的记录：
 *
 *
 *
 *
 * @author 黄军鹏
 * @since 2020-07-15
 */
@Service
public class HistoryBillServiceImpl extends ServiceImpl<HistoryBillMapper, HistoryBill> implements IHistoryBillService {
    private final String CREDIT_CARD_ID = "CREDIT_CARD_ID";
    @Autowired
    private HistoryBillMapper historyBillMapper;
    @Autowired
    private IRepaymentRecordService repaymentRecordService;

/**
 * 查询信用卡账号的历史账单
 * @param creditCardId
 * @return
 */
    public HistoryBill getHistoryBill(String creditCardId){
     return    historyBillMapper.selectOne(new QueryWrapper<HistoryBill>().eq(CREDIT_CARD_ID,creditCardId));
    }

/**
 * 计算应还的总金额
 * @param creditCardId,date 还款日
 * @returndate
 */
    public BigDecimal getTotalBill(String creditCardId,Date date ){
            HistoryBill historyBill = getHistoryBill(creditCardId);
            BigDecimal remainingBillAmount = historyBill.getRemainingBillAmount();
            BigDecimal addCount = remainingBillAmount.add(historyBill.getInitialInterest());//计算未还总金额和前置利息
            int days = DateUtils.getIntervalDays(date);
            BigDecimal interest = remainingBillAmount.multiply(new BigDecimal("0.0005")).multiply(new BigDecimal(days + ""));
        System.out.println(addCount);
        System.out.println(interest);
        return addCount.add(interest);
            
    }

/**
 * 在出账后重置历史账单 ：重置策略：金额清0 ，利息清0 ，更改日期为出账日
 * @param creditCardId
 * @param
 * @return
 */
    public boolean resetHistoryBill(String creditCardId,Date date){
        BigDecimal reset = new BigDecimal("0");
        HistoryBill historyBill = getHistoryBill(creditCardId);
        historyBill.setInstallmentBillAmount(reset).setSimpleBillAmount(reset)
                .setPaidAmount(reset).setRemainingBillAmount(reset)
                .setInitialInterest(reset).setUpdateTime(date);
        int result = historyBillMapper.updateById(historyBill);
        while (result==1){
            return true;
        }
        return false;
    }



public boolean repayBill(String creditCardId,Date repayDate,BigDecimal amount,String cardNo){
        HistoryBill historyBill = getHistoryBill(creditCardId);
        BigDecimal totalBill = getTotalBill(creditCardId, historyBill.getUpdateTime());
        if (totalBill.compareTo(amount)==0) {
            System.out.println("余款全部还清");
            //添加还款记录
            boolean repayment = repaymentRecordService.repayment(creditCardId, historyBill.getHistoryBillId(), PaymentType.历史账单还款,
                    amount, cardNo);
            boolean b = resetHistoryBill(creditCardId, repayDate);
            if (repayment&&b){
                return true;
            }
            return false;
        }
        
        BigDecimal remainingBillAmount = historyBill.getRemainingBillAmount();//未还金额
        int days = DateUtils.getIntervalDays(historyBill.getUpdateTime());
        BigDecimal interest = remainingBillAmount.multiply(new BigDecimal("0.0005")).multiply(new BigDecimal(days + ""));//二期利息
        
        if (amount.compareTo(remainingBillAmount)==-1) {
            boolean repayment = repaymentRecordService.repayment(creditCardId, historyBill.getHistoryBillId(), PaymentType.历史账单还款,
                    amount, cardNo);
            historyBill.setRemainingBillAmount(remainingBillAmount.subtract(amount)).setPaidAmount(historyBill.getPaidAmount().add(amount));
            historyBill.setUpdateTime(repayDate).setInitialInterest(historyBill.getInitialInterest().add(interest));
            int i = historyBillMapper.updateById(historyBill);
            return repayment&&(i==1);
        }else {
            BigDecimal restAmount = amount.subtract(remainingBillAmount);
            BigDecimal restInterest = historyBill.getInitialInterest().add(interest).subtract(restAmount);
            boolean repayment = repaymentRecordService.repayment(creditCardId, historyBill.getHistoryBillId(), PaymentType.历史账单还款,
                    amount, cardNo);
            historyBill.setRemainingBillAmount(new BigDecimal("0")).setPaidAmount(historyBill.getPaidAmount().add(amount));
            historyBill.setUpdateTime(repayDate).setInitialInterest(restInterest);
            int i = historyBillMapper.updateById(historyBill);
            return repayment&&(i==1);
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
